package cn.iocoder.an.module.mold.service.stockiorecords;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.text.SimpleDateFormat;
import java.util.*;
import cn.iocoder.an.module.mold.controller.admin.stockiorecords.vo.*;
import cn.iocoder.an.module.mold.dal.dataobject.stockiorecords.StockIoRecordsDO;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import cn.iocoder.an.module.mold.dal.mysql.stockiorecords.StockIoRecordsMapper;
import static cn.iocoder.an.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.an.module.mold.enums.ErrorCodeConstants.*;

/**
 * 模具出入库记录 Service 实现类
 *
 * @author ykk
 */
@Service
@Validated
public class StockIoRecordsServiceImpl implements StockIoRecordsService {

    @Resource
    private StockIoRecordsMapper stockIoRecordsMapper;

    @Override
    public Long createStockIoRecords(StockIoRecordsSaveReqVO createReqVO) {
        // 插入
        StockIoRecordsDO stockIoRecords = BeanUtils.toBean(createReqVO, StockIoRecordsDO.class);
        stockIoRecordsMapper.insert(stockIoRecords);

        // 返回
        return stockIoRecords.getId();
    }

    @Override
    public void updateStockIoRecords(StockIoRecordsSaveReqVO updateReqVO) {
        // 校验存在
        validateStockIoRecordsExists(updateReqVO.getId());
        // 更新
        StockIoRecordsDO updateObj = BeanUtils.toBean(updateReqVO, StockIoRecordsDO.class);
        stockIoRecordsMapper.updateById(updateObj);
    }

    @Override
    public void deleteStockIoRecords(Long id) {
        // 校验存在
        validateStockIoRecordsExists(id);
        // 删除
        stockIoRecordsMapper.deleteById(id);
    }

    @Override
        public void deleteStockIoRecordsListByIds(List<Long> ids) {
        // 删除
        stockIoRecordsMapper.deleteByIds(ids);
        }


    private void validateStockIoRecordsExists(Long id) {
        if (stockIoRecordsMapper.selectById(id) == null) {
            throw exception(STOCK_IO_RECORDS_NOT_EXISTS);
        }
    }

    @Override
    public StockIoRecordsDO getStockIoRecords(Long id) {
        return stockIoRecordsMapper.selectById(id);
    }

    @Override
    public PageResult<StockIoRecordsDO> getStockIoRecordsPage(StockIoRecordsPageReqVO pageReqVO) {
        return stockIoRecordsMapper.selectPage(pageReqVO);
    }

    // 获取一个未使用的单号
    public String getLastUnUsedCode(Integer inOrOut) {
        // 获取指定出入库类型的最新一条记录的code
        String lastCode = stockIoRecordsMapper.getLastCode(inOrOut);
        // 获取当前日期字符串（格式：yyyyMMdd）
        String currentDate = new SimpleDateFormat("yyyyMMdd").format(new Date());
        if (lastCode != null && lastCode.startsWith(currentDate)) {
            // 如果最后一条记录是今天的，则在该编号基础上+1
            int number = Integer.parseInt(lastCode.substring(8)) + 1;
            return currentDate + String.format("%04d", number);
        } else {
            // 如果最后一条记录不是今天的，则生成今天的第一个编号
            return currentDate + "0001";
        }
    }


}